package com.draven.mall.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.draven.mall.entity.Rider;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.Map;

/**
 * 骑手Mapper接口
 */
@Mapper
public interface RiderMapper extends BaseMapper<Rider> {

    /**
     * 分页查询骑手评价列表
     * 
     * @param page 分页参数
     * @param riderId 骑手ID
     * @param rating 评分筛选
     * @return 评价列表
     */
    @Select({
        "<script>",
        "SELECT r.id, r.order_id, r.user_id, r.rating, r.comment, r.created_time, ",
        "o.order_no, u.username as user_name, u.avatar as user_avatar ",
        "FROM rider_rating r ",
        "LEFT JOIN tb_order o ON r.order_id = o.id ",
        "LEFT JOIN tb_user u ON r.user_id = u.id ",
        "WHERE r.rider_id = #{riderId} ",
        "<if test='rating != null and rating != \"\"'>",
        "AND r.rating = #{rating} ",
        "</if>",
        "ORDER BY r.created_time DESC",
        "</script>"
    })
    IPage<Map<String, Object>> selectRiderRatings(Page<Map<String, Object>> page, 
                                                  @Param("riderId") Long riderId, 
                                                  @Param("rating") String rating);
    
    /**
     * 统计骑手各星级评价数量
     * 
     * @param riderId 骑手ID
     * @return 各星级评价数量统计
     */
    @Select({
        "SELECT IFNULL(rating, 0) as rating, COUNT(*) as count ",
        "FROM rider_rating ",
        "WHERE rider_id = #{riderId} ",
        "GROUP BY rating"
    })
    Map<String, Long> countRiderRatingsByScore(@Param("riderId") Long riderId);
}
